<template>
  <div id="app-container" style="padding:30px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="章节名称">
        <el-input v-model="chapterQuery.name" placeholder="章节名称"></el-input>
      </el-form-item>
      <el-form-item label="章节状态">
        <el-select v-model="chapterQuery.status" placeholder="章节状态">
          <el-option label="已入库" value="1"></el-option>
          <el-option label="未入库" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="更新时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="选择日期" v-model="chapterQuery.date1" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-date-picker type="date" placeholder="选择日期" v-model="chapterQuery.date2" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query()">查询</el-button>
        <el-button type="primary" @click="reset()">清空</el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="chapterList"
        stripe
        style="width: 100%">
        <el-table-column prop="book.name" label="书籍名" width="180"/>
        <el-table-column prop="name" label="章节名" width="180"/>
        <el-table-column prop="number" label="章节序号" width="180"/>
        <el-table-column prop="status" label="章节状态" width="180">
          <template slot-scope="scope">
            {{ scope.row.status === 1 ? '已入库' : '未入库'}}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="180"/>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <router-link :to="'/bms/copyright/chapterupdate/'+scope.row.id">
              <el-button v-if="scope.row.status ===0" type="text" >修改</el-button>
              <el-button v-else disabled type="text">修改</el-button>
            </router-link>
            <el-button v-if="scope.row.status ===0" type="text" @click="deleteChapter(scope.row.id)">删除</el-button>
            <el-button v-else disabled type="text">删除</el-button>
            <el-button v-if="scope.row.status === 0" type="text" @click="upOrDownChapter(scope.row.id, 1)">入库</el-button>
            <el-button v-else disabled type="text">入库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 分页 -->
    <el-pagination
      :page-size="pageSize"
      :current-page="pageNo"
      :total="total"
      :pager-count="11"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="query()">
    </el-pagination>
  </div>
</template>

<script>
import chapter from '@/api/copyrightin/chapter.js'
export default {
  data() { // 自定义变量，初始化变量
    return {
      chapterList: null, // ## 数据列表
      chapterQuery: {}, // ##查询条件封装对象
      total: 0, // ## 总记录数
      pageNo: 1, // ## 页码
      pageSize: 10, // ## 每页显示记录数
      currentTime: '' // ## 当前时间
    }
  },
  created() { // 页面渲染之前执行
    this.query()
  },
  methods: { // 自定义函数
    query(pageNo = 1) { // 查询
      this.pageNo = pageNo
      // 调用后端接口查询数据
      chapter.query(this.pageNo, this.pageSize, this.chapterQuery)
        .then(response => {
          // 赋值
          this.chapterList = response.data.items
          this.total = response.data.total
          this.currentTime = this.format(new Date(), 'yyyy-MM-dd HH:mm:ss')
        })
    },
    reset() { // 清空查询条件内容
      // 清空数据
      this.chapterQuery = {}
      // 重新查询
      this.query()
    },
    deleteChapter(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        chapter.delete(id)
          .then(response => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.query()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    upOrDownChapter(id, status) { // 书籍上下架
      this.$confirm('此操作将改变章节状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        chapter.upOrDownChapter(id, status)
          .then(response => {
            this.$message({
              type: 'success',
              message: '变更状态完成'
            })
            this.query()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
